home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / dblround.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  823 b   |  43 lines

  1. /*
  2. **  DBLROUND.C - Rounds a double to the nearest whole number
  3. **  public domain by Ross Cottrell
  4. */
  5.  
  6. #include <float.h>
  7. #include <limits.h>
  8. #include <assert.h>
  9.  
  10. typedef enum {FALSE, TRUE} boolean;
  11. double round(double x)
  12. {
  13.       boolean flag;
  14.  
  15.       if (TRUE == (flag = (x < 0.0)))
  16.             x = -x;
  17.       assert(1 == FLT_ROUNDS);
  18.       x += 1.0 / DBL_EPSILON;
  19.       x -= 1.0 / DBL_EPSILON;
  20.       return ((flag) ? -x : x);
  21. }
  22.  
  23. #ifdef TEST
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27.  
  28. main(int argc, char *argv[])
  29. {
  30.       double val;
  31.       char *dummy;
  32.  
  33.       while (--argc)
  34.       {
  35.             val = strtod((const char *)(*(++argv)), &dummy);
  36.             printf("round(%g) = ", val);
  37.             printf("%.12g\n", round(val));
  38.       }
  39.       return EXIT_SUCCESS;
  40. }
  41.  
  42. #endif /* TEST */
  43.